package com.wd.health.wxapi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wd.health.R;
import com.wd.health.model.bean.WxLoginBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.view.activity.mine.LoginActivity;
import com.wd.health.view.activity.mine.MoneyFinishActivity;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    private IWXAPI api;
    private String APP_ID = "wxe3fcbe8a55cd33ff";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_w_x_pay_entry);
        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
/*
          0 支付成功
         -1 发生错误 可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
         -2 用户取消 发生场景：用户不支付了，点击取消，返回APP。
         */

        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            // 根据返回码
            int code = baseResp.errCode;
            //发送广播通知页面支付结果
            System.out.println("-------------code"+code);
            Intent intent=new Intent("wechat.pay.Broadcast");
            intent.putExtra("message", code);
            sendBroadcast(intent);

            switch (code) {
                case 0:
                    // 去本地确认支付结果
                    Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(WXPayEntryActivity.this, MoneyFinishActivity.class);
                    startActivity(intent1);
                    finish();
                    break;
                case -2:
                    Toast.makeText(this, "支付已取消", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                default:
                    Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
        }
    }

}